package com.kevin.Code.Math;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author Vinlee Xiao
 * @Classname BasicCalculator
 * @Description Leetcode 224. 基本计算器 难度 困难
 * @Date 2021/12/18 15:10
 * @Version 1.0
 */
public class BasicCalculator {

    /**
     * 明确计算规则
     * 代码写的较乱
     * 思路不活跃 考虑的问题不全面
     * 全部看作相加运算
     *
     * @param s
     * @return
     */
    public int calculate(String s) {
        //得到去除头尾空格后的字符串
        String s1 = s.trim();
        int len = s1.length();

        if (len == 0) {
            return 0;
        }
        //最终的结果
        int result = 0;
        //用于存储数字
        char[] ch = s1.toCharArray();
        int index = 0;
        int digit = 0;
        //
        boolean preNegative = false;
        while (index < len) {
            //如果碰到非数字类型的字符串

            if (!Character.isDigit(ch[index])) {

                if (preNegative) {
                    result = digit == 0 ? result : result - digit;
                } else {
                    result = digit == 0 ? result : digit + result;
                }
                //将digit置为零
                digit = 0;
                //进行符号的判定
                if (ch[index] == '-') {
                    preNegative = true;
                } else if (ch[index] == '+') {
                    preNegative = false;
                }
                index++;
                continue;
            }

            //经过前面的过滤
            while (index < len && Character.isDigit(ch[index])) {
                digit = digit * 10 + ch[index] - '0';
                index++;
            }
        }
        if (digit != 0) {
            result = result + digit;
        }

        return result;
    }

    /**
     * 关键点和难点 括号前面的符号会影响括号里面的运算
     *
     * @param s
     * @return
     */
    public int calculate1(String s) {

        String s1 = s.trim();
        int len = s1.length();

        if (len == 0) {
            return 0;
        }
        //最终的结果
        int result = 0;
        //用于存储数字
        char[] ch = s1.toCharArray();
        int index = 0;
        int digit = 0;
        //标记符号正负
        int sign = 1;
        Deque<Integer> operatorDeque = new LinkedList<>();
        operatorDeque.push(sign);

        while (index < len) {
            if (ch[index] == ' ') {
                index++;
            } else if (ch[index] == '+') {
                sign = operatorDeque.peekLast();
                index++;
            } else if (ch[index] == '-') {
                sign = -operatorDeque.peekLast();
                index++;
            } else if (ch[index] == '(') {
                //将左括号之前的符号进入栈中
                operatorDeque.addLast(sign);
                index++;
            } else if (ch[index] == ')') {
                operatorDeque.pollLast();
                index++;
            } else {
                while (index < len && Character.isDigit(ch[index])) {
                    digit = digit * 10 + ch[index] - '0';
                    index++;
                }
                result = result + sign * digit;
                digit = 0;
            }
        }
        return result;
    }


    public static void main(String[] args) {
        BasicCalculator basicCalculator = new BasicCalculator();
        int i = basicCalculator.calculate1("(1+(4+5+2)-3)+(6+8)");
        System.out.println(i);
    }
}
